package com.example.projectjava.controller;

import com.example.projectjava.service.CategoryService;
import com.example.projectjava.util.JsonResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@Tag(name = "产品类别接口")
public class CategoryController {

    @Autowired   //此注解的作用：是spring的基本特性，它将项目中运行过程中的JdbcTemplate,自动创建并实例化传入
    JdbcTemplate jdbcTemplate;

    @Autowired
    CategoryService categoryService;

    @ResponseBody
    @GetMapping("/insertCategory")
    @Operation(summary = "添加产品类别")
    @Parameters({
            @Parameter(name ="category_id",description="类别ID(不允许为空)",required = true),
            @Parameter(name ="category_name",description="产品名称(不允许为空)",required = true),
            @Parameter(name ="product_count",description="种类包含子类数(不允许为空)",required = true)
    })
    public JsonResponse insertCategory(String category_id,String category_name,String product_count) {
        JsonResponse jsonResponse = new JsonResponse();
        categoryService.insertCategory(category_id, category_name, product_count);    //调用service层的userRegister方法判断用户名手机号是否重复
        jsonResponse.setCode(0);
        jsonResponse.setMsg("插入成功");
        return jsonResponse;
    }

    @ResponseBody
    @GetMapping("/deleteCategory")
    @Operation(summary = "删除产品类别")
    @Parameters({
            @Parameter(name ="category_id",description="类别ID(不允许为空)",required = true)
    })
    public JsonResponse deleteCategory(String category_id) {
        JsonResponse jsonResponse = new JsonResponse();
        try {
            categoryService.deleteCategory(category_id);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg("删除失败:" + e.getMessage());
        }
        return jsonResponse;
    }

    @ResponseBody
    @GetMapping("/queryAllCategory")
    @Operation(summary = "查询所有产品类别")
    public JsonResponse queryAllCategory() {
        JsonResponse jsonResponse = new JsonResponse();
        List<Map<String, Object>> categoryList = categoryService.queryAllCategory();    //调用service层的queryAllUser方法查询所有用户信息
        jsonResponse.setCode(0);
        jsonResponse.setMsg("查询成功");
        jsonResponse.setData(categoryList);
        return jsonResponse;
    }

    @ResponseBody
    @GetMapping("/updateCategory")
    @Operation(summary = "更新产品类别")
    @Parameters({
            @Parameter(name ="category_id",description="类别ID(不允许为空)",required = true),
            @Parameter(name ="category_name",description="产品名称(不允许为空)",required = true),
            @Parameter(name ="product_count",description="种类包含子类数(不允许为空)",required = true)
    })
    public JsonResponse updateCategory(String category_id, String category_name, String product_count) {
        JsonResponse jsonResponse = new JsonResponse();
        try {
            categoryService.updateCategory(category_id,category_name, product_count);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg("更新失败:" + e.getMessage());
        }
        return jsonResponse;
    }

    @ResponseBody
    @GetMapping("/queryProductByCategory")
    @Operation(summary = "查询产品类别下的所有产品")
    @Parameters({
            @Parameter(name ="category_id",description="类别ID(不允许为空)",required = true)
    })
    public JsonResponse queryProductByCategory(String category_id) {
        JsonResponse jsonResponse = new JsonResponse();
        List<Map<String, Object>> productList = categoryService.queryProductByCategory(category_id);    //调用service层的queryAllUser方法查询所有用户信息
        jsonResponse.setCode(0);
        jsonResponse.setMsg("查询成功");
        jsonResponse.setData(productList);
        return jsonResponse;
    }
}
